﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UpdateServer
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hiddenappID.Value = Request.QueryString["APPID"];
                hiddenappVersion.Value = DateTime.Now.ToString("yyyyMMddHHmmss");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (hiddenappID.Value.Trim() == "")
            {
                UpdateService.Alert("应用编码错误.");
                return;
            }

            if (hiddenappVersion.Value.Trim() == "")
            {
                UpdateService.Alert("版本号生成错误.");
                return;
            }
            if (!fileUpdate.HasFile)
            {
                UpdateService.Alert("请上传更新文件包.");
                return;
            }
            if (!fileUpdate.FileName.ToUpper().EndsWith(".ZIP"))
            {
                UpdateService.Alert("更新包智能是ZIP格式.");
                return;
            }

            if (txttNote.Value.Trim()=="")
            {
                UpdateService.Alert("请填写更新日志.");
                return;
            }
            string appid = hiddenappID.Value.ToUpper();
            string fullFileName = Server.MapPath("UploadFiles/" + appid + "/" + hiddenappVersion.Value + ".zip");
            string newFilesPaht = Server.MapPath("UploadFiles/" + appid + "/" + hiddenappVersion.Value);


            if (!System.IO.Directory.Exists(newFilesPaht))
            {
                System.IO.Directory.CreateDirectory(newFilesPaht);
            }

            if (System.IO.File.Exists(fullFileName))
            {
                System.IO.FileInfo fin = new System.IO.FileInfo(fullFileName);
                fin.Delete();
            }
            fileUpdate.SaveAs(fullFileName);

        
            ServerData.T_VERSION_FILEDataTable fileData = new ServerData.T_VERSION_FILEDataTable();
            ServerData.T_APPS_VERSIONDataTable versionData = new ServerData.T_APPS_VERSIONDataTable();
            ServerData.T_APPSDataTable app = new ServerData.T_APPSDataTable();
            if (txtUpdateUrl.Text.Trim() == "")
            {

                txtUpdateUrl.Text = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, "/") + "UploadFiles/" + appid + "/" + hiddenappVersion.Value + ".zip";
            }
            if (UpdateService.UnZipFile(fullFileName, newFilesPaht))
            {
                System.IO.DirectoryInfo dirs = new System.IO.DirectoryInfo(newFilesPaht);
                System.IO.FileInfo[] files = dirs.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
                foreach (System.IO.FileInfo fle in files)
                {
                    fileData.AddT_VERSION_FILERow(Guid.NewGuid().ToString(), fle.FullName.Replace(newFilesPaht + "\\", ""), fle.Extension, hiddenappVersion.Value);
                }
                versionData.AddT_APPS_VERSIONRow(hiddenappVersion.Value, hiddenappID.Value, hiddenappVersion.Value + ".zip", DateTime.Now, txtUpdateUrl.Text.Trim(), txttNote.Value.Trim());
                ServerDataTableAdapters.T_APPSTableAdapter adapp = new UpdateServer.ServerDataTableAdapters.T_APPSTableAdapter();
                ServerDataTableAdapters.T_VERSION_FILETableAdapter adfile = new UpdateServer.ServerDataTableAdapters.T_VERSION_FILETableAdapter();
                ServerDataTableAdapters.T_APPS_VERSIONTableAdapter adv = new UpdateServer.ServerDataTableAdapters.T_APPS_VERSIONTableAdapter();
                app = adapp.GetDataByAPPID(hiddenappID.Value);
                app[0].LASTVERSION = hiddenappVersion.Value;
                app[0].LASTDATE = DateTime.Now;
                adv.Update(versionData);
                adfile.Update(fileData);
                adapp.Update(app);
            }
            else
            {
                UpdateService.Alert("验证解压文件失败.");
                return;
            }
            Response.Redirect("Default.aspx");
        }
    }
}
